const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function seedProducts() {
  try {
    console.log('开始填充商品数据...');

    // 检查分类是否存在
    const categories = await prisma.category.findMany();
    if (categories.length === 0) {
      console.log('请先运行种子脚本创建分类');
      return;
    }

    // 删除不再需要的虚拟商品
    const virtualProducts = ['青岛啤酒', '哈啤', '科罗娜', '1664白啤', '精酿IPA', '水果啤酒'];
    console.log('检查需要手动删除的虚拟商品...');
    
    for (const productName of virtualProducts) {
      const product = await prisma.product.findFirst({
        where: { name: productName }
      });
      
      if (product) {
        console.log(`发现虚拟商品: ${productName}，ID: ${product.id}`);
        console.log(`建议手动删除或在管理界面将其设为不可见`);
        // 不自动删除，仅打印信息
      }
    }
    
    // 商品数据
    const products = [
      {
        name: '比利时小麦',
        description: '异域风味理想宝，曾为远方酿酒师',
        price: 13.0,
        categoryId: 3,
        imageUrl: '/images/products/1.jpg',
        status: true
      },
      {
        name: '德式小麦',
        description: '传统经典风味，酿酒传承再现',
        price: 11.0,
        categoryId: 3,
        imageUrl: '/images/products/2.jpg',
        status: true
      },
      {
        name: '绿豆精酿',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/3.jpg',
        status: true
      },
      {
        name: '浓香荞麦',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/4.jpg',
        status: true
      },
      {
        name: '世涛黑啤',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/5.jpg',
        status: true
      },
      {
        name: '双倍IPA',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/6.jpg',
        status: true
      },
      {
        name: '葡萄芝士',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/7.jpg',
        status: true
      },
      {
        name: '香甜荔枝',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/8.jpg',
        status: true
      },
      {
        name: '女神百香果',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/9.jpg',
        status: true
      },
      {
        name: '龙井小麦',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/10.jpg',
        status: true
      },
      {
        name: '原浆鲜啤',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/11.jpg',
        status: true
      },
      {
        name: '西柚茉莉',
        description: '创新口味，清爽宜人',
        price: 15.0,
        categoryId: 3,
        imageUrl: '/images/products/12.jpg',
        status: true
      }
    ];

    // 检查是否已有商品数据，避免覆盖用户自定义的商品
    const existingProductCount = await prisma.product.count();

    if (existingProductCount > 0) {
      console.log(`数据库中已有 ${existingProductCount} 个商品，为避免覆盖用户数据，跳过商品种子数据填充`);
      console.log('如需重新填充商品数据，请先清空商品表或手动删除不需要的商品');
      return;
    }

    // 创建商品 - 仅在没有商品数据时执行
    for (const product of products) {
      // 创建新商品
      const newProduct = await prisma.product.create({
        data: product
      });
      console.log(`创建商品: ${newProduct.name}, ID: ${newProduct.id}`);
    }

    console.log('商品数据填充完成');
  } catch (error) {
    console.error('填充商品数据失败:', error);
  } finally {
    await prisma.$disconnect();
  }
}

seedProducts(); 